var express = require('express');
var mysql = require('mysql');
var utils = require('../utils');
var router = express.Router();

var database = require('../database.config');
var connection = mysql.createConnection(database);
connection.connect();

function getUserInfo(userName, req, cb) {
  var sql = `SELECT * FROM chat_user u WHERE chatCode='${userName}' or telephone='${userName}'`
  connection.query(sql, function (error, result) {
    if (error) throw error;
    if (result.length > 0) {
      result.forEach(element => {
        element.base = 'http://' + req.host + ':' + global.port;
      });
    }
    cb(result);
  });
}

router.post('/login', function (req, res, next) {
  const params = req.body;
  var sql = `SELECT * FROM chat_account WHERE 
  (chatCode='${params.userName}' OR telephone='${params.userName}') 
  AND password='${params.password}'`
  connection.query(sql, function (error, result) {
    if (error) throw error;
    if (result.length <= 0) {
      res.send(utils.sendData(null, 0, '没有查到该用户'));
    } else {
      getUserInfo(params.userName, req, (userInfo) => {
        res.send(utils.sendData(userInfo[0]));
      });
    }
  });
});

router.post('/register', function (req, res, next) {
  const params = req.body;
  if (!params.chatCode) {
    res.send(utils.sendData('缺少社交code', 500));
    return;
  }
  if (!params.password) {
    res.send(utils.sendData('缺少密码', 500));
    return;
  }
  // 创建账户
  var sql = `INSERT INTO chat_account 
  (chatCode, telephone, password)
  VALUES (?, ?, ?)`
  const addSql = [params.chatCode, params.telephone, params.password];
  connection.query(sql, addSql, function (error, result) {
    if (error) throw error;
    res.send(utils.sendData('操作成功！'));
  });

  // 创建用户信息
  var insertSql = `INSERT INTO chat_user 
  (chatCode, telephone, userName, sex, headImg, userProfile, province, city) 
  VALUES 
  (?, ?, ?, ?, ?, ?, ?, ?)`
  const addInsertSql = [params.chatCode, params.telephone, null, null, null, null, null, null];
  connection.query(insertSql, addInsertSql, function (error, result) {
    if (error) throw error;
  });
});

router.post('/updateUserInfo', function (req, res, next) {
  const params = req.body;
  if (!params.chatCode && !params.telephone) {
    res.send('缺少标识', 500);
    return;
  }

  var sql = `UPDATE chat_user SET userName = ?,sex = ?,headImg = ?,userProfile = ?,province = ?,city = ? WHERE chatCode = ?`;
  var modSql = [params.userName, params.sex, params.headImg, params.userProfile, params.province, params.city, params.chatCode];
  connection.query(sql, modSql, function (error, result) {
    if (error) throw error;
    res.send(utils.sendData('操作成功！'));
  });
})


/**
 * params
 * chatCode, 社交号或者手机号
 */
router.post('/findUserDetail', function (req, res, next) {
  const params = req.body;
  getUserInfo(params.chatCode, req, (result) => {
    if (result.length <= 0) {
      res.send(utils.sendData(null, 0, '没有查到该用户'));
    } else {
      res.send(utils.sendData(result[0]));
    }
  })
});

module.exports = router;